#!/usr/bin/env bash

# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License;
# you may not use this file except in compliance with the Elastic License.

# Script to setup environment variables and deployer config to run CI tasks.
#
# Usage: setenvconfig ENV

set -eu
set -o pipefail

ROOT=$(dirname $0)/..

write_env()             { cat > $ROOT/.env; }
write_deployer_config() { cat > $ROOT/deployer-config.yml; }

# detect dev local execution
if [[ ${BUILD_ID:-} == "" ]]; then
  BUILD_NUMBER=0 # keeping all build_numbers as numbers
  BUILD_TAG=local-dev-$(date +%s)t
  # need to be declared but not required for local execution
  VAULT_ROLE_ID="dev"
  VAULT_SECRET_ID="dev"
fi

# main

environment=${1:-""}
case $environment in

######################################
  e2e/aks)
######################################

write_env <<ENV
IMG_SUFFIX=-ci
E2E_REGISTRY_NAMESPACE=eck-ci

GO_TAGS=release
export LICENSE_PUBKEY=/go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
TEST_LICENSE=/go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS=/go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

OPERATOR_IMAGE=$JKS_PARAM_OPERATOR_IMAGE

PIPELINE=e2e/aks
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=aks
CLUSTER_NAME=$BUILD_TAG
TEST_OPTS=-race
ENV
write_deployer_config <<CFG
id: aks-ci
overrides:
  clusterName: $BUILD_TAG
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
CFG
;;


######################################
  e2e/gke-k8s-versions)
######################################

clusterVersion=$2
clusterName=$3

write_env <<ENV
IMG_SUFFIX=-ci
E2E_REGISTRY_NAMESPACE=eck-ci

GO_TAGS=release
export LICENSE_PUBKEY=/go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
TEST_LICENSE=/go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS=/go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

OPERATOR_IMAGE=$JKS_PARAM_OPERATOR_IMAGE

PIPELINE=e2e/gke-k8s-versions
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=gke
CLUSTER_NAME=${clusterName}
TEST_OPTS=-race
ENV
write_deployer_config <<CFG
id: gke-ci
overrides:
  operation: create
  kubernetesVersion: "${clusterVersion}"
  clusterName: ${clusterName}
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  gke:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  e2e/kind-k8s-versions)
######################################

kindNodeImage=$2
kindVersion=$3
ipFamily=$4

write_env <<ENV
IMG_SUFFIX=-ci
E2E_REGISTRY_NAMESPACE=eck-ci

GO_TAGS=release
export LICENSE_PUBKEY=/go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
TEST_LICENSE=/go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS=/go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

OPERATOR_IMAGE=$JKS_PARAM_OPERATOR_IMAGE

PIPELINE=e2e/kind-k8s-versions
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=kind
CLUSTER_NAME=kind-$BUILD_NUMBER
TEST_OPTS=-race
E2E_TEST_ENV_TAGS="kind,$ipFamily"
ENV
write_deployer_config <<CFG
id: kind-ci
overrides:
  clusterName: eck-e2e-$BUILD_NUMBER
  clientVersion: ${kindVersion}
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  kind:
    nodeImage: ${kindNodeImage}
    ipFamily: ${ipFamily}
CFG
;;

######################################
  e2e/master)
######################################

write_env <<ENV
IMG_SUFFIX=-ci
REGISTRY_NAMESPACE=eck-snapshots
E2E_REGISTRY_NAMESPACE=eck-ci

GO_TAGS=release
export LICENSE_PUBKEY=/go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
TEST_LICENSE=/go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS=/go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

PIPELINE=e2e/master
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=gke
CLUSTER_NAME=eck-e2e-$BUILD_NUMBER
TEST_OPTS=-race
ENV
write_deployer_config <<CFG
id: gke-ci
overrides:
  clusterName: eck-e2e-$BUILD_NUMBER
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  gke:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  e2e/resilience)
######################################

clusterName=$2

write_env <<ENV
E2E_DEPLOY_CHAOS_JOB=true
IMG_SUFFIX=-ci
REGISTRY_NAMESPACE=eck-snapshots
E2E_REGISTRY_NAMESPACE=eck-ci

GO_TAGS=release
export LICENSE_PUBKEY=/go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
export E2E_DEPLOY_CHAOS_JOB=true
TEST_LICENSE=/go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS=/go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

PIPELINE=e2e/resilience
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=gke
OPERATOR_IMAGE=$JKS_PARAM_OPERATOR_IMAGE
CLUSTER_NAME=${clusterName}
TEST_OPTS=-race
ENV
write_deployer_config <<CFG
id: gke-ci
overrides:
  clusterName: ${clusterName}
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  gke:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  e2e/ocp)
######################################

write_env <<ENV
IMG_SUFFIX=-ci
E2E_REGISTRY_NAMESPACE=eck-ci

GO_TAGS=release
export LICENSE_PUBKEY=/go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
TEST_LICENSE=/go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS=/go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

OPERATOR_IMAGE=$JKS_PARAM_OPERATOR_IMAGE

PIPELINE=e2e/ocp
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=ocp
CLUSTER_NAME=ocp4-ci-$BUILD_NUMBER
TEST_OPTS=-race
ENV
write_deployer_config <<CFG
id: ocp-ci
overrides:
  clusterName: ocp4-ci-$BUILD_NUMBER
  clientVersion: $2
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  ocp:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  e2e/ocp3)
######################################

write_env <<ENV
IMG_SUFFIX=-ci
E2E_REGISTRY_NAMESPACE=eck-ci

GO_TAGS=release
export LICENSE_PUBKEY=/go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
TEST_LICENSE=/go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS=/go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

OPERATOR_IMAGE=$JKS_PARAM_OPERATOR_IMAGE

PIPELINE=e2e/ocp3
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=ocp3
CLUSTER_NAME=eck-ocp3-$BUILD_NUMBER
TEST_OPTS=-race
ENV
write_deployer_config <<CFG
id: ocp3-ci
overrides:
  clusterName: eck-ocp3-$BUILD_NUMBER
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  ocp3:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  e2e/eks)
######################################

write_env <<ENV
IMG_SUFFIX=-ci
E2E_REGISTRY_NAMESPACE=eck-ci

GO_TAGS=release
export LICENSE_PUBKEY=/go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
TEST_LICENSE=/go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
MONITORING_SECRETS=/go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

OPERATOR_IMAGE=$JKS_PARAM_OPERATOR_IMAGE

PIPELINE=e2e/eks
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=eks
CLUSTER_NAME=$BUILD_TAG
TEST_OPTS=-race
ENV
write_deployer_config <<CFG
id: eks-ci
overrides:
  clusterName: $BUILD_TAG
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
CFG
;;

######################################
  e2e/eks-arm)
######################################

write_env <<ENV
IMG_SUFFIX=-ci
E2E_REGISTRY_NAMESPACE=eck-ci

GO_TAGS=release
E2E_TAGS=es kb apm ent beat agent
export LICENSE_PUBKEY=/go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
# TEST_LICENSE=/go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json disabled b/c https://github.com/elastic/elasticsearch/issues/68083
# MONITORING_SECRETS=/go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json disabled b/c beats cannot run on ARM

OPERATOR_IMAGE=$JKS_PARAM_OPERATOR_IMAGE

PIPELINE=e2e/eks-arm
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=eks
CLUSTER_NAME=${BUILD_TAG}-arm
KUBERNETES_VERSION=default
TEST_OPTS=-race
E2E_TEST_ENV_TAGS="arch:arm"
ENV
write_deployer_config <<CFG
id: eks-arm-ci
overrides:
  clusterName: ${BUILD_TAG}-arm
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
CFG
;;

######################################
  e2e/stack-versions)
######################################

clusterName=$2
stackVersion=$3
isSnapshotVersion=$(case $stackVersion in (*-SNAPSHOT)  echo Yes;; esac)
testLicensePKeyPath=$(case $isSnapshotVersion in (Yes) echo "/go/src/github.com/elastic/cloud-on-k8s/.ci/dev-private.key";; esac)
operatorImage=${OPERATOR_IMAGE:-$JKS_PARAM_OPERATOR_IMAGE}

write_env <<ENV
IMG_SUFFIX=-ci
E2E_REGISTRY_NAMESPACE=eck-ci
IS_SNAPSHOT_BUILD=${isSnapshotVersion}

TEST_LICENSE=/go/src/github.com/elastic/cloud-on-k8s/.ci/test-license.json
TEST_LICENSE_PKEY_PATH=$testLicensePKeyPath
MONITORING_SECRETS=/go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

OPERATOR_IMAGE=${operatorImage}
E2E_STACK_VERSION=${stackVersion}

PIPELINE=e2e/stack-versions
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=gke
CLUSTER_NAME=${clusterName}
TEST_OPTS=-race
ENV
write_deployer_config <<CFG
id: gke-ci
overrides:
  operation: create
  clusterName: ${clusterName}
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  gke:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  pr)
######################################

write_env <<ENV
IMG_SUFFIX=-ci
REGISTRY_NAMESPACE=eck-snapshots
E2E_REGISTRY_NAMESPACE=eck-ci

TESTS_MATCH=TestSmoke
MONITORING_SECRETS=/go/src/github.com/elastic/cloud-on-k8s/.ci/monitoring-secrets.json

PIPELINE=pr
BUILD_NUMBER=$BUILD_NUMBER
E2E_PROVIDER=gke
CLUSTER_NAME=eck-pr-$BUILD_NUMBER
TEST_OPTS=-race
ENV
write_deployer_config <<CFG
id: gke-ci
overrides:
  clusterName: eck-pr-$BUILD_NUMBER
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  gke:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  build)
######################################

  WORKSPACE=${WORKSPACE:-$ROOT}

  # valid the existence of the $WORKSPACE/VERSION file
  if [[ ! -f $WORKSPACE/VERSION ]]; then
    echo "$WORKSPACE/VERSION not found"
    exit
  fi

  # choose between a nightly or a release build based on TAG_NAME
  if [[ ${TAG_NAME:-} == "" ]]; then

###############
# nightly build
###############

# IMG_VERSION is generated

write_env <<ENV
REGISTRY_NAMESPACE=eck-snapshots
IMG_SUFFIX=

GO_TAGS=release
export LICENSE_PUBKEY=/go/src/github.com/elastic/cloud-on-k8s/.ci/license.key

SNAPSHOT=true
IMG_VERSION=$(cat $WORKSPACE/VERSION)-$(date +%F)-$(git rev-parse --short --verify HEAD)
ENV

  else

###############
# release build
###############

write_env <<ENV
REGISTRY_NAMESPACE=eck
IMG_SUFFIX=

GO_TAGS=release
export LICENSE_PUBKEY=/go/src/github.com/elastic/cloud-on-k8s/.ci/license.key

SNAPSHOT=false
IMG_VERSION=$TAG_NAME
ENV

  fi
;;

######################################
  dev/build)
######################################

write_env <<ENV
IMG_SUFFIX=-dev-ci
REGISTRY_NAMESPACE=eck-snapshots
E2E_REGISTRY_NAMESPACE=eck-ci
IS_SNAPSHOT_BUILD=yes
GO_TAGS=release
export LICENSE_PUBKEY=/go/src/github.com/elastic/cloud-on-k8s/.ci/license.key
ENV
;;


######################################
  cleanup/gke)
######################################

write_deployer_config <<CFG
id: gke-ci
overrides:
  operation: delete
  clusterName: $JKS_PARAM_GKE_CLUSTER
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  gke:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  cleanup/aks)
######################################

write_deployer_config <<CFG
id: aks-ci
overrides:
  operation: delete
  clusterName: $BUILD_TAG
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
CFG
;;

######################################
  cleanup/eks)
######################################

write_deployer_config <<CFG
id: eks-ci
overrides:
  operation: delete
  clusterName: $BUILD_TAG
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
CFG
;;

######################################
  cleanup/eks-arm)
######################################

write_deployer_config <<CFG
id: eks-arm-ci
overrides:
  operation: delete
  clusterName: ${BUILD_TAG}-arm
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
CFG
;;

######################################
  cleanup/ocp)
######################################

write_deployer_config <<CFG
id: ocp-ci
overrides:
  operation: delete
  clusterName: ocp4-ci-$BUILD_NUMBER
  clientVersion: $2
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  ocp:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  cleanup/ocp3)
######################################

write_deployer_config <<CFG
id: ocp3-ci
overrides:
  operation: delete
  clusterName: eck-ocp3-$BUILD_NUMBER
  vaultInfo:
    address: $VAULT_ADDR
    roleId: $VAULT_ROLE_ID
    secretId: $VAULT_SECRET_ID
  ocp3:
    gCloudProject: $GCLOUD_PROJECT
CFG
;;

######################################
  *)
    echo -e "\nError: invalid environment\n"
    echo 'Environments available:'
    grep -ro '^\s\s*[a-z].*)$' $0 | sed "s/)//"
    exit 1
  ;;

esac
